package KK.net
{
	import KK.Cache.CacheManager;
	import KK.Data.Config;
	import KK.Data.GameData;
	import KK.EMVC.Control.Control;
	import KK.Event.GameSystemEvent;
	import KK.GameBase.MSGEvent;
	import KK.Global.Game;
	import KK.System.ApplicationManager;
	import KK.System.System;
	import KK.Time.G_Date;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	
	public class ResourceManager extends Control
	{
		
		static private const START:int=1;
		
		static private var _instance:ResourceManager;
		
		
		private var assetsLen:int;
		private var currentID:int;
		private var state:int;
		public function ResourceManager(singleton:Singleton)
		{
			//TODO: implement function
			super();
			if(_instance){
				throw new Error("this singleton class pless using Instance get Instance.");
			}
			_instance=this;
		}
		
		/**
		 * 开始  
		 * 
		 */
		public function Run():void{
		}
		
		/**
		 * 初始化加载模块  
		 * 
		 */
		public function InitModule():void{
			if(state!=START){
				Loader(GameData.SwfURL+GameData.InitXML+"?"+G_Date.GetInstance().time,initXML);
				state=START;
			}
		}
		
		/**
		 * 加载数据类型 
		 * @param url
		 * @param Complete
		 * 
		 */
		public function Loader(url:String,Complete:Function):void{
			System.AddLoader(GameData.SwfURL+GameData.InitXML,Complete,null,onIOError,onSecurityError);
			
		}
		
		/**
		 * 加载器  
		 * @param url
		 * @param Complete
		 * 
		 */
		public function SwfLoader(url:String,Complete:Function):void{
			System.AddSwfLoader(url,Complete,null,onIOError,onSecurityError,onProgress);
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * 初始化xml加载完毕  
		 * @param event
		 * 
		 */
		private function initXML(event:Event):void{
			currentID=0;
			var xml:XML=new XML(event.target.data);
			var len:int;
			var i:int=0;
			len=xml.Application.app.length();
			assetsLen+=len;
			while(i<len){
				SwfLoader(xml.Application.app[i].@src,LoadedApp);
				
				
				i++;
			}
			
			i=0;
			len=xml.Image.img.length();
			assetsLen+=len;
			while(i<len){
				
				SwfLoader(xml.Image.img[i].@src+"?name="+xml.Image.img[i].@name,LoadedImager);
				i++;
			}
		}
		
		/**
		 * 加载完毕图片 
		 * @param event
		 * 
		 */
		private function LoadedImager(event:Event):void{
			CacheManager.AddCache(event.currentTarget.parameters.name,event.currentTarget.content);
			checkAssets();
		}
		
		
		/**
		 * 加载完毕SWF
		 * @param event
		 * 
		 */
		private function LoadedApp(event:Event):void{
			ApplicationManager.Add(event.target.applicationDomain);
			LogInfo(event.target.loaderURL);
			checkAssets();
		}
		
		
		/**
		 * 检查是否结束  
		 * 
		 */
		private function checkAssets():void{
			
			if(++currentID==assetsLen){
				Config.InitConfig();
				Publish(new MSGEvent(GameSystemEvent.RESOURCE_LOADED));
				
			}
			
		}
		
		
		
		
		
		
		
		
		
		
		/**
		 * 加载进度  
		 * @param event
		 * 
		 */
		private function onProgress(event:Event):void{
			
		}
		
		
		/**
		 * 安全策略错误  
		 * @param event
		 * 
		 */
		private function onSecurityError(event:Event):void{
			LogInfo("数据加载失败 ，服务器缺少安全策略文件");
			
		}
		
		/**
		 * io加载错误  
		 * @param event
		 * 
		 */
		private function onIOError(event:IOErrorEvent):void{
			LogInfo("数据加载失败，url地址错误 ",event.text);
		}
		/**
		 * 加载 
		 * 
		 */
		static public function AddSwfLoader(url:String,Complete:Function ):void{
			Instance.SwfLoader(url,Complete);
		}
		
		/**
		 *  开始初始化加载内容  
		 * 
		 */
		static public function Start():void{
			
			Instance.Run();
			Instance.InitModule();
		}
		
		/**
		 * 获取单例 
		 * @return 
		 * 
		 */
		static public function get Instance():ResourceManager{
			if(!_instance){
				new ResourceManager(new Singleton());
			}
			return _instance;
		}
	}
}

class Singleton{
	
}